package com.example.filecabinet.controller;

import com.example.filecabinet.biz.BizController;
import com.example.filecabinet.constant.RecordConstant;
import com.example.filecabinet.dto.req.BizUserQueryReq;
import com.example.filecabinet.dto.req.GeneralStoreReq;
import com.example.filecabinet.entity.Lattice_info;
import com.example.filecabinet.entity.Record_info;
import com.example.filecabinet.util.QueryFreeFells;
import com.thfdcsoft.framework.common.http.dto.BaseHttpRspn;
import com.thfdcsoft.framework.common.json.jackson.JacksonFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 普通用户控制器
 *
 * @author wangy
 */
@Controller
public class GeneralUserController {

    private static final Logger logger = LoggerFactory.getLogger(GeneralUserController.class);

    /**
     * 用户查询待存业务
     *
     * @return
     */
    @PostMapping("/queryStoreBusiness")
    @ResponseBody
    public String queryStoreBusiness(BizUserQueryReq bizUserQueryReq) {
        return BizController.getBusinessInfo(bizUserQueryReq);
    }

    @RequestMapping("/queryGeneral")
    public String index() {
        return "generalStoreList";
    }

    /**
     * 用户存储业务
     */
    @PostMapping(value = "/storeInfo")
    @ResponseBody
    public String storeInfo(GeneralStoreReq generalStoreReq) {
        logger.info(generalStoreReq.toString() + generalStoreReq.getDeviceNumber());
        // 查询是否已经有该条记录
        List<Lattice_info> lattice_infos = QueryFreeFells.queryBusinessExist(generalStoreReq);
        System.out.println("lattice_infos.size()" + lattice_infos.size());
        BaseHttpRspn result = new BaseHttpRspn(false);
        if (lattice_infos.size() != 0) {
            result.setResult(true);
            result.setRespJson(JacksonFactory.writeJson(lattice_infos.get(0)));
        } else {
            boolean flag = QueryFreeFells.getFreeFellNumber(generalStoreReq.getDeviceNumber());
            if (flag) {
                Lattice_info lattice_info = QueryFreeFells.getLatticeNumber(generalStoreReq.getDeviceNumber());
                lattice_info.setBusinessType(generalStoreReq.getBusinessType());
                lattice_info.setBusinessNumber(generalStoreReq.getBusinessNumber());
                result.setResult(true);
                result.setRespJson(JacksonFactory.writeJson(lattice_info));
            } else {
                result.setResult(false);
                result.setRespMsg("无空格可用");
            }
        }
        logger.info("返回值：" + JacksonFactory.writeJson(result));
        return JacksonFactory.writeJson(result);
    }
}
